package com.example.graduate.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.graduate.dto.AccountSerialRequest;
import com.example.graduate.dto.RechargeRequest;
import com.example.graduate.dto.UserInfoRequest;
import com.example.graduate.dto.WithDrawRequest;
import com.example.graduate.env.ResponseCodeEnum;
import com.example.graduate.response.DataResponse;
import com.example.graduate.service.UserService;
import com.example.graduate.token.JudgeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/user",produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 充值接口
     * @param rechargeRequest
     * @return
     */
    @PostMapping("/recharge")
    @JudgeToken
    public DataResponse recharge(@RequestBody RechargeRequest rechargeRequest){
        log.info("【充值请求】{}:", JSONObject.toJSONString(rechargeRequest));
        if (ObjectUtils.isEmpty(rechargeRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        DataResponse dataResponse = userService.addBalance(rechargeRequest);
        log.info("【充值请求】响应信息", JSONObject.toJSONString(dataResponse));
        return dataResponse;
    }

    /**
     * 提现接口
     * @param withdrawRequest
     * @return
     */
    @PostMapping("/withdraw")
    @JudgeToken
    public DataResponse withdraw(@RequestBody WithDrawRequest withdrawRequest){
        log.info("【提现请求】{}:", JSONObject.toJSONString(withdrawRequest));
        if (ObjectUtils.isEmpty(withdrawRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        DataResponse dataResponse = userService.subBalance(withdrawRequest);
        log.info("【提现请求】响应信息：{}", JSONObject.toJSONString(dataResponse));
        return dataResponse;
    }

    /**
     *  充值提现流水
     */
    @PostMapping("/serial")
    @JudgeToken
    public DataResponse accountSerial(@RequestBody AccountSerialRequest accountSerialRequest){
        log.info("【获取金额流水请求】：{}",JSONObject.toJSONString(accountSerialRequest));
        if (ObjectUtils.isEmpty(accountSerialRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        DataResponse dataResponse = userService.getAccountSerial(accountSerialRequest);
        log.info("【获取金额流水请求】响应信息：{}", JSONObject.toJSONString(dataResponse));
        return dataResponse;
    }

    @PostMapping("/userInfo")
    @JudgeToken
    public DataResponse getUserInfo(@RequestBody UserInfoRequest userInfoRequest){
        log.info("【用户信息请求】",JSONObject.toJSONString(userInfoRequest));
        if (ObjectUtils.isEmpty(userInfoRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        DataResponse dataResponse = userService.getUserInfo(userInfoRequest);
        log.info("【充值提现流水请求】响应信息", JSONObject.toJSONString(dataResponse));
        return dataResponse;
    }
}
